<template>
  <div class="app-container">
    <toolbar @changePeriod="handleChangePeriod" @refresh="handleRefresh">
      <template v-slot:right>
        <span style="float: left"
          ><i class="el-icon-caret-left"></i
          ><account-select style="width: 220px"></account-select
          ><i class="el-icon-caret-right"></i
        ></span>
      </template>
    </toolbar>
    <div style="text-align: center; padding-top: 10px; padding-bottom: 10px">
      <div style="font-size: 16px; font-weight: bold">明细分类账</div>
      <div>{{ filter.startPeriod + " 至 " + filter.endPeriod }}</div>
    </div>
    <div>
      <el-table
        :data="datas"
        style="width: 100%; margin-bottom: 20px"
        row-key="accountId"
        border
        stripe
        fit
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="日期" width="180">
          <template slot-scope="scope">
            {{ scope.row.date | date }}
          </template>
        </el-table-column>
        <el-table-column prop="number" label="凭证号" width="180">
        </el-table-column>
        <el-table-column prop="summary" label="摘要" width="180">
        </el-table-column>
        <el-table-column label="借方">
          <template slot-scope="scope">
            {{ scope.row.debitAmount | money }}
          </template></el-table-column
        >
        <el-table-column label="贷方">
          <template slot-scope="scope">
            {{ scope.row.creditAmount | money }}
          </template></el-table-column
        >
        <el-table-column label="方向">
          <template slot-scope="scope">
            {{ scope.row.balanceDirection }}
          </template></el-table-column
        >
        <el-table-column label="余额">
          <template slot-scope="scope">
            {{ scope.row.balance }}
          </template></el-table-column
        >
      </el-table>
    </div>
  </div>
</template>
<script>
import { subsidiaryledgerData } from "@/api/report";
import toolbar from "@/components/Bar/reportToolbar";
import accountSelect from "@/components/Select/accountSelect.vue";
export default {
  components: {
    toolbar,
    accountSelect,
  },
  data() {
    return {
      datas: [],
      filter: {
        startPeriod: null,
        endPeriod: null,
        accountId: null,
      },
    };
  },
  methods: {
    getData() {
      this.datas = [];
      subsidiaryledgerData(this.filter).then((res) => {
        if (!res) {
          this.datas = [];
          return;
        }
        this.datas = res;
      });
    },
    handleChangePeriod(values) {
      if (values && values.length == 2) {
        this.filter.startPeriod = values[0].toPeriod();
        this.filter.endPeriod = values[1].toPeriod();
      }
    },
    handleRefresh() {
      this.getData();
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.getData();
    });
  },
  watch: {
    periods: {
      handler(values) {
        if (values && values.length == 2) {
          this.filter.startPeriod = values[0].toPeriod();
          this.filter.endPeriod = values[1].toPeriod();
        }
      },
    },
  },
};
</script>
<style scoped></style>
